<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>disposable_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_disposable_disposable_test.js.html">disposable_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.DisposableTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.DisposableTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.recordFunction&#39;);
<a name="line21"></a>var d1, d2;
<a name="line22"></a>
<a name="line23"></a>// Sample subclass of goog.Disposable.
<a name="line24"></a>
<a name="line25"></a>function DisposableTest() {
<a name="line26"></a>  goog.Disposable.call(this);
<a name="line27"></a>  this.element = document.getElementById(&#39;someElement&#39;);
<a name="line28"></a>}
<a name="line29"></a>goog.inherits(DisposableTest, goog.Disposable);
<a name="line30"></a>
<a name="line31"></a>DisposableTest.prototype.disposeInternal = function() {
<a name="line32"></a>  DisposableTest.superClass_.disposeInternal.call(this);
<a name="line33"></a>  delete this.element;
<a name="line34"></a>};
<a name="line35"></a>
<a name="line36"></a>// Class that doesn&#39;t inherit from goog.Disposable, but implements the
<a name="line37"></a>// disposable interface via duck typing.
<a name="line38"></a>
<a name="line39"></a>function DisposableDuck() {
<a name="line40"></a>  this.element = document.getElementById(&#39;someElement&#39;);
<a name="line41"></a>}
<a name="line42"></a>
<a name="line43"></a>DisposableDuck.prototype.dispose = function() {
<a name="line44"></a>  delete this.element;
<a name="line45"></a>};
<a name="line46"></a>
<a name="line47"></a>// Class which calls dispose recursively.
<a name="line48"></a>
<a name="line49"></a>function RecursiveDisposable() {
<a name="line50"></a>  this.disposedCount = 0;
<a name="line51"></a>}
<a name="line52"></a>goog.inherits(RecursiveDisposable, goog.Disposable);
<a name="line53"></a>
<a name="line54"></a>RecursiveDisposable.prototype.disposeInternal = function() {
<a name="line55"></a>  ++this.disposedCount;
<a name="line56"></a>  assertEquals(&#39;Disposed too many times&#39;, 1, this.disposedCount);
<a name="line57"></a>  this.dispose();
<a name="line58"></a>};
<a name="line59"></a>
<a name="line60"></a>// Test methods.
<a name="line61"></a>
<a name="line62"></a>function setUp() {
<a name="line63"></a>  d1 = new goog.Disposable();
<a name="line64"></a>  d2 = new DisposableTest();
<a name="line65"></a>}
<a name="line66"></a>
<a name="line67"></a>function tearDown() {
<a name="line68"></a>  goog.Disposable.MONITORING_MODE = goog.Disposable.MonitoringMode.OFF;
<a name="line69"></a>  goog.Disposable.INCLUDE_STACK_ON_CREATION = true;
<a name="line70"></a>  goog.Disposable.instances_ = {};
<a name="line71"></a>  d1.dispose();
<a name="line72"></a>  d2.dispose();
<a name="line73"></a>}
<a name="line74"></a>
<a name="line75"></a>function testConstructor() {
<a name="line76"></a>  assertFalse(d1.isDisposed());
<a name="line77"></a>  assertFalse(d2.isDisposed());
<a name="line78"></a>  assertEquals(document.getElementById(&#39;someElement&#39;), d2.element);
<a name="line79"></a>}
<a name="line80"></a>
<a name="line81"></a>function testDispose() {
<a name="line82"></a>  assertFalse(d1.isDisposed());
<a name="line83"></a>  d1.dispose();
<a name="line84"></a>  assertTrue(&#39;goog.Disposable instance should have been disposed of&#39;,
<a name="line85"></a>      d1.isDisposed());
<a name="line86"></a>
<a name="line87"></a>  assertFalse(d2.isDisposed());
<a name="line88"></a>  d2.dispose();
<a name="line89"></a>  assertTrue(&#39;goog.DisposableTest instance should have been disposed of&#39;,
<a name="line90"></a>      d2.isDisposed());
<a name="line91"></a>}
<a name="line92"></a>
<a name="line93"></a>function testDisposeInternal() {
<a name="line94"></a>  assertNotUndefined(d2.element);
<a name="line95"></a>  d2.dispose();
<a name="line96"></a>  assertUndefined(&#39;goog.DisposableTest.prototype.disposeInternal should &#39; +
<a name="line97"></a>      &#39;have deleted the element reference&#39;, d2.element);
<a name="line98"></a>}
<a name="line99"></a>
<a name="line100"></a>function testDisposeAgain() {
<a name="line101"></a>  d2.dispose();
<a name="line102"></a>  assertUndefined(&#39;goog.DisposableTest.prototype.disposeInternal should &#39; +
<a name="line103"></a>      &#39;have deleted the element reference&#39;, d2.element);
<a name="line104"></a>  // Manually reset the element to a non-null value, and call dispose().
<a name="line105"></a>  // Because the object is already marked disposed, disposeInternal won&#39;t
<a name="line106"></a>  // be called again.
<a name="line107"></a>  d2.element = document.getElementById(&#39;someElement&#39;);
<a name="line108"></a>  d2.dispose();
<a name="line109"></a>  assertNotUndefined(&#39;disposeInternal should not be called again if the &#39; +
<a name="line110"></a>      &#39;object has already been marked disposed&#39;, d2.element);
<a name="line111"></a>}
<a name="line112"></a>
<a name="line113"></a>function testDisposeWorksRecursively() {
<a name="line114"></a>  new RecursiveDisposable().dispose();
<a name="line115"></a>}
<a name="line116"></a>
<a name="line117"></a>function testStaticDispose() {
<a name="line118"></a>  assertFalse(d1.isDisposed());
<a name="line119"></a>  goog.dispose(d1);
<a name="line120"></a>  assertTrue(&#39;goog.Disposable instance should have been disposed of&#39;,
<a name="line121"></a>      d1.isDisposed());
<a name="line122"></a>
<a name="line123"></a>  assertFalse(d2.isDisposed());
<a name="line124"></a>  goog.dispose(d2);
<a name="line125"></a>  assertTrue(&#39;goog.DisposableTest instance should have been disposed of&#39;,
<a name="line126"></a>      d2.isDisposed());
<a name="line127"></a>
<a name="line128"></a>  var duck = new DisposableDuck();
<a name="line129"></a>  assertNotUndefined(duck.element);
<a name="line130"></a>  goog.dispose(duck);
<a name="line131"></a>  assertUndefined(&#39;goog.dispose should have disposed of object that &#39; +
<a name="line132"></a>      &#39;implements the disposable interface&#39;, duck.element);
<a name="line133"></a>}
<a name="line134"></a>
<a name="line135"></a>function testStaticDisposeOnNonDisposableType() {
<a name="line136"></a>  // Call goog.dispose() with various types and make sure no errors are
<a name="line137"></a>  // thrown.
<a name="line138"></a>  goog.dispose(true);
<a name="line139"></a>  goog.dispose(false);
<a name="line140"></a>  goog.dispose(null);
<a name="line141"></a>  goog.dispose(undefined);
<a name="line142"></a>  goog.dispose(&#39;&#39;);
<a name="line143"></a>  goog.dispose([]);
<a name="line144"></a>  goog.dispose({});
<a name="line145"></a>
<a name="line146"></a>  function A() {}
<a name="line147"></a>  goog.dispose(new A());
<a name="line148"></a>}
<a name="line149"></a>
<a name="line150"></a>function testMonitoringFailure() {
<a name="line151"></a>  function BadDisposable() {};
<a name="line152"></a>  goog.inherits(BadDisposable, goog.Disposable);
<a name="line153"></a>
<a name="line154"></a>  goog.Disposable.MONITORING_MODE =
<a name="line155"></a>      goog.Disposable.MonitoringMode.PERMANENT;
<a name="line156"></a>
<a name="line157"></a>  var badDisposable = new BadDisposable;
<a name="line158"></a>  assertArrayEquals(&#39;no disposable objects registered&#39;, [],
<a name="line159"></a>      goog.Disposable.getUndisposedObjects());
<a name="line160"></a>  assertThrows(&#39;the base ctor should have been called&#39;,
<a name="line161"></a>      goog.bind(badDisposable.dispose, badDisposable));
<a name="line162"></a>}
<a name="line163"></a>
<a name="line164"></a>function testGetUndisposedObjects() {
<a name="line165"></a>  goog.Disposable.MONITORING_MODE =
<a name="line166"></a>      goog.Disposable.MonitoringMode.PERMANENT;
<a name="line167"></a>
<a name="line168"></a>  var d1 = new DisposableTest();
<a name="line169"></a>  var d2 = new DisposableTest();
<a name="line170"></a>  assertSameElements(&#39;the undisposed instances&#39;, [d1, d2],
<a name="line171"></a>      goog.Disposable.getUndisposedObjects());
<a name="line172"></a>
<a name="line173"></a>  d1.dispose();
<a name="line174"></a>  assertSameElements(&#39;1 undisposed instance left&#39;, [d2],
<a name="line175"></a>      goog.Disposable.getUndisposedObjects());
<a name="line176"></a>
<a name="line177"></a>  d1.dispose();
<a name="line178"></a>  assertSameElements(&#39;second disposal of the same object is no-op&#39;, [d2],
<a name="line179"></a>      goog.Disposable.getUndisposedObjects());
<a name="line180"></a>
<a name="line181"></a>  d2.dispose();
<a name="line182"></a>  assertSameElements(&#39;all objects have been disposed of&#39;, [],
<a name="line183"></a>      goog.Disposable.getUndisposedObjects());
<a name="line184"></a>}
<a name="line185"></a>
<a name="line186"></a>function testClearUndisposedObjects() {
<a name="line187"></a>  goog.Disposable.MONITORING_MODE =
<a name="line188"></a>      goog.Disposable.MonitoringMode.PERMANENT;
<a name="line189"></a>
<a name="line190"></a>  var d1 = new DisposableTest();
<a name="line191"></a>  var d2 = new DisposableTest();
<a name="line192"></a>  d2.dispose();
<a name="line193"></a>  goog.Disposable.clearUndisposedObjects();
<a name="line194"></a>  assertSameElements(&#39;no undisposed object in the registry&#39;, [],
<a name="line195"></a>      goog.Disposable.getUndisposedObjects());
<a name="line196"></a>
<a name="line197"></a>  assertThrows(&#39;disposal after clearUndisposedObjects()&#39;, function() {
<a name="line198"></a>    d1.dispose();
<a name="line199"></a>  });
<a name="line200"></a>
<a name="line201"></a>  // d2 is already disposed of, the redisposal shouldn&#39;t throw error.
<a name="line202"></a>  d2.dispose();
<a name="line203"></a>}
<a name="line204"></a>
<a name="line205"></a>function testRegisterDisposable() {
<a name="line206"></a>  var d1 = new DisposableTest();
<a name="line207"></a>  var d2 = new DisposableTest();
<a name="line208"></a>
<a name="line209"></a>  d1.registerDisposable(d2);
<a name="line210"></a>  d1.dispose();
<a name="line211"></a>
<a name="line212"></a>  assertTrue(&#39;d2 should be disposed when d1 is disposed&#39;, d2.isDisposed());
<a name="line213"></a>}
<a name="line214"></a>
<a name="line215"></a>function testDisposeAll() {
<a name="line216"></a>  var d1 = new DisposableTest();
<a name="line217"></a>  var d2 = new DisposableTest();
<a name="line218"></a>
<a name="line219"></a>  goog.disposeAll(d1, d2);
<a name="line220"></a>
<a name="line221"></a>  assertTrue(&#39;d1 should be disposed&#39;, d1.isDisposed());
<a name="line222"></a>  assertTrue(&#39;d2 should be disposed&#39;, d2.isDisposed());
<a name="line223"></a>}
<a name="line224"></a>
<a name="line225"></a>function testDisposeAllRecursive() {
<a name="line226"></a>  var d1 = new DisposableTest();
<a name="line227"></a>  var d2 = new DisposableTest();
<a name="line228"></a>  var d3 = new DisposableTest();
<a name="line229"></a>  var d4 = new DisposableTest();
<a name="line230"></a>
<a name="line231"></a>  goog.disposeAll(d1, [[d2], d3, d4]);
<a name="line232"></a>
<a name="line233"></a>  assertTrue(&#39;d1 should be disposed&#39;, d1.isDisposed());
<a name="line234"></a>  assertTrue(&#39;d2 should be disposed&#39;, d2.isDisposed());
<a name="line235"></a>  assertTrue(&#39;d3 should be disposed&#39;, d3.isDisposed());
<a name="line236"></a>  assertTrue(&#39;d4 should be disposed&#39;, d4.isDisposed());
<a name="line237"></a>}
<a name="line238"></a>
<a name="line239"></a>function testCreationStack() {
<a name="line240"></a>  if (!new Error().stack)
<a name="line241"></a>    return;
<a name="line242"></a>  goog.Disposable.MONITORING_MODE =
<a name="line243"></a>      goog.Disposable.MonitoringMode.PERMANENT;
<a name="line244"></a>  var disposableStack = new DisposableTest().creationStack;
<a name="line245"></a>  // Check that the name of this test function occurs in the stack trace.
<a name="line246"></a>  assertNotEquals(-1, disposableStack.indexOf(&#39;testCreationStack&#39;));
<a name="line247"></a>}
<a name="line248"></a>
<a name="line249"></a>function testMonitoredWithoutCreationStack() {
<a name="line250"></a>  if (!new Error().stack)
<a name="line251"></a>    return;
<a name="line252"></a>  goog.Disposable.MONITORING_MODE =
<a name="line253"></a>      goog.Disposable.MonitoringMode.PERMANENT;
<a name="line254"></a>  goog.Disposable.INCLUDE_STACK_ON_CREATION = false;
<a name="line255"></a>  var d1 = new DisposableTest();
<a name="line256"></a>
<a name="line257"></a>  // Check that it is tracked, but not with a creation stack.
<a name="line258"></a>  assertUndefined(d1.creationStack);
<a name="line259"></a>  assertSameElements(&#39;the undisposed instance&#39;, [d1],
<a name="line260"></a>      goog.Disposable.getUndisposedObjects());
<a name="line261"></a>}
<a name="line262"></a>
<a name="line263"></a>function testOnDisposeCallback() {
<a name="line264"></a>  var callback = goog.testing.recordFunction();
<a name="line265"></a>  d1.addOnDisposeCallback(callback);
<a name="line266"></a>  assertEquals(&#39;callback called too early&#39;, 0, callback.getCallCount());
<a name="line267"></a>  d1.dispose();
<a name="line268"></a>  assertEquals(&#39;callback should be called once on dispose&#39;,
<a name="line269"></a>      1, callback.getCallCount());
<a name="line270"></a>}
<a name="line271"></a>
<a name="line272"></a>function testOnDisposeCallbackOrder() {
<a name="line273"></a>  var invocations = [];
<a name="line274"></a>  var callback = function(str) {
<a name="line275"></a>    invocations.push(str);
<a name="line276"></a>  };
<a name="line277"></a>  d1.addOnDisposeCallback(goog.partial(callback, &#39;a&#39;));
<a name="line278"></a>  d1.addOnDisposeCallback(goog.partial(callback, &#39;b&#39;));
<a name="line279"></a>  goog.dispose(d1);
<a name="line280"></a>  assertArrayEquals(&#39;callbacks should be called in chronological order&#39;,
<a name="line281"></a>      [&#39;a&#39;, &#39;b&#39;], invocations);
<a name="line282"></a>}
<a name="line283"></a>
<a name="line284"></a>function testInteractiveMonitoring() {
<a name="line285"></a>  var d1 = new DisposableTest();
<a name="line286"></a>  goog.Disposable.MONITORING_MODE =
<a name="line287"></a>      goog.Disposable.MonitoringMode.INTERACTIVE;
<a name="line288"></a>  var d2 = new DisposableTest();
<a name="line289"></a>
<a name="line290"></a>  assertSameElements(&#39;only 1 undisposed instance tracked&#39;, [d2],
<a name="line291"></a>      goog.Disposable.getUndisposedObjects());
<a name="line292"></a>
<a name="line293"></a>  // No errors should be thrown.
<a name="line294"></a>  d1.dispose();
<a name="line295"></a>
<a name="line296"></a>  assertSameElements(&#39;1 undisposed instance left&#39;, [d2],
<a name="line297"></a>      goog.Disposable.getUndisposedObjects());
<a name="line298"></a>
<a name="line299"></a>  d2.dispose();
<a name="line300"></a>  assertSameElements(&#39;all disposed&#39;, [],
<a name="line301"></a>      goog.Disposable.getUndisposedObjects());
<a name="line302"></a>}
</pre>


</body>
</html>
